package cn.xd.core.response;

import java.util.function.Consumer;

/**
 * @author: mirzaLin
 * @data: 2023/11/19
 * @description: Object组合
 **/
public class OptionalObjectResult<T> {

    private final IntegerResult integerResult;

    private final ObjectResult<T> objectResult;

    private OptionalObjectResult(IntegerResult integerResult, ObjectResult<T> objectResult) {
        this.integerResult = integerResult;
        this.objectResult = objectResult;
    }

    public static <T> OptionalObjectResult<T> of(int integerResult, T objectResult) {
        return new OptionalObjectResult<>(IntegerResult.of(integerResult), ObjectResult.of(objectResult));
    }

    public ObjectResult<T> failThrow(String msg) throws RuntimeException {
        integerResult.failThrow(msg);
        return objectResult;
    }

    public ObjectResult<T> fail(Function function) {
        integerResult.fail(function);
        return objectResult;
    }

    public ObjectResult<T> failThrow() {
        integerResult.failThrow();
        return objectResult;
    }

    public void success(Function function) {
        integerResult.success(function);
    }

    public void success(Consumer<T> consumer) {
        integerResult.success(() -> objectResult.isNotEmpty(consumer));
    }

    public void successAndFailThrow(Consumer<T> consumer) {
        integerResult.success(() -> objectResult.isNotEmpty(consumer));
        integerResult.failThrow();
    }

    public T get() {
        return objectResult.get();
    }
}
